package cn.han.tickets;

public class TestRunnable {
    public static void main(String[] args) {
        TicketRunnable t1 = new TicketRunnable();
        Thread thread = new Thread(t1);
        Thread thread1 = new Thread(t1);
        Thread thread2 = new Thread(t1);
        Thread thread3 = new Thread(t1);
        thread.start();
        thread1.start();
        thread2.start();
        thread3.start();

    }
}

class TicketRunnable implements Runnable {
    //由于自定义类对象值创建了一次，所以票数被所有线程对象Thread类的对象共享
    int tickets = 100;

    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+tickets--);
            if (tickets<=0)break;
        }
    }
}